/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */ 

package com.nokia.example.wordpress.networking;

import java.util.Vector;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Parses the users blogs server response, creates an array of Blog objects.
 */
public class UsersBlogsParser extends Parser {

    /**
     * Result vector of Blogs.
     */
    public Vector blogs = new Vector();
    /**
     * Current blog being parsed.
     */
    private Blog blog;

    public void startElement(String uri, String localName, String qName, Attributes attributes) {
        super.startElement(uri,localName,qName,attributes);
    
        if (qName.equals("struct")) {
            blog = new Blog();
        }
    }

    public void endElement(String uri, String localName, String qName) {
        String chars = getChars();

        if (qName.equals("struct")) {
            blogs.addElement(blog);
        } else if (qName.equals("name")) {
            if (chars.equals("blogid") || chars.equals("blogName") || chars.equals("xmlrpc") || chars.equals("faultString")) {
                current = chars;
            }
        } else if (qName.equals("string")) {
            if (current.equals("blogid")) {
                blog.blogId = chars;
                current = "";
            } else if (current.equals("blogName")) {
                blog.name = chars;
                current = "";
            } else if (current.equals("xmlrpc")) {
                blog.xmlrpcUrl = chars;
                current = "";
            } else if (current.equals("faultString")) {
                blog.faultString = chars;
                current = "";
            }
        }
    }
}

/* Server response:
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<array><data>
<value><struct>
<member><name>isAdmin</name><value><boolean>1</boolean></value></member>
<member><name>url</name><value><string>http://blogusername.wordpress.com/</string></value></member>
<member><name>blogid</name><value><string>13777908</string></value></member>
<member><name>blogName</name><value><string>blogusername&amp;#039;s Blog</string></value></member>
<member><name>xmlrpc</name><value><string>http://blogusername.wordpress.com/xmlrpc.php</string></value></member>
</struct></value>
</data></array>
</value>
</param>
</params>
</methodResponse>
 */